import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/pay_notice_bean.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///确认收款 收款明细
class UiPayNoticeDetailsInfo {
  static Widget build(BuildContext context, List<PayNoticeDetail>? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).PaymentDetails; //收款明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(
      context,
      data,
    ));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<PayNoticeDetail>? data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        if (data != null && data.length > 0) {
          var postion = 1;
          data.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(
                children: cells,
                decoration: BoxDecoration(
                    color: AppColors.getColorWithTableRow(postion))));
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(150),
          },
          border: AppStyle.getTableBorder(),
          textDirection: TextDirection.ltr,
          children: children,
        );
        var width = 2500.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).ContractCode)); //合同编号
    children.add(_addItem(AppStrings.of(context).OutCode)); //合同外部编号
    children.add(_addItem(AppStrings.of(context).ProjectName)); //项目名称
    children.add(_addItem(AppStrings.of(context).SalesmanName)); //销售经理
    children.add(_addItem(AppStrings.of(context).MerchandiserName)); //客服经理
    children
        .add(_addItem(AppStrings.of(context).LastDeliveryDateTime)); //最后发货时间
    children.add(_addItem(AppStrings.of(context).OverdueDateTimeName)); //收款超期时间
    children.add(_addItem(AppStrings.of(context).Cutrate)); //提成扣除比列
    children.add(_addItem(AppStrings.of(context).ColTypeName)); //收款类型
    children.add(_addItem(AppStrings.of(context).ColMethodName)); //收款方式
    children.add(_addItem(AppStrings.of(context).Receivables)); //实付金额
    children.add(_addItem(AppStrings.of(context).AmountInArear)); //开票应收款总额
    children.add(_addItem(AppStrings.of(context).Arrears)); //开票应收款余额
    children.add(_addItem(AppStrings.of(context).AdvancePaymentAmount)); //预付款总额
    children
        .add(_addItem(AppStrings.of(context).AdvancePaymentArrears)); //预付款余额
    children.add(_addItem(AppStrings.of(context).AdvancePaymentType)); //预付款冲抵类型
    children.add(_addItem(AppStrings.of(context).AdvancePaymentOffset)); //预付款冲抵
    children.add(_addItem(AppStrings.of(context).Erate)); //汇率
    children.add(_addItem(AppStrings.of(context).ColAmount)); //收款金额
    children.add(_addItem(AppStrings.of(context).Intro)); //备注
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      PayNoticeDetail element, StateSetter setState) {
    List<Widget> children = [];
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.ContractCode)); //合同编号
    children.add(_addItem(element.OutCode)); //合同外部编号
    children.add(_addItem(element.ProjectName)); //项目名称
    children.add(_addItem(element.SalesmanName)); //销售经理
    children.add(_addItem(element.MerchandiserName)); //客服经理
    children.add(_addItem(element.LastDeliveryDateTime)); //最后发货时间
    children.add(_addItem(element.OverdueDateTimeName, isBlue: true, onTap: () {
      var OverdueRateList = element.OverdueRateList;
      var OverdueDateTimeName = element.OverdueDateTimeName;
      if (StringUtils.isNotEmpty(OverdueRateList)) {
        AppDialog.showDialogWithRollerPicker<OverdueRate>(context,
                datas: OverdueRateList, value: OverdueDateTimeName)
            .then((value) {
          //print('=====================${value?.FactoryName}');
          if (value != null) {
            setState(() {
              element.OverdueDateTimeName = value.OverdueDateTimeName;
              element.Cutrate = value.Cutrate;
            });
          }
        });
      }
    })); //fixme 收款超期时间
    children.add(_addItem(element.Cutrate)); //fixme 提成扣除比列
    children.add(_addItem(element.ColTypeName)); //收款类型
    children.add(_addItem(element.ColMethodName)); //收款方式
    children.add(_addItem(element.Receivables)); //实付金额
    children.add(_addItem(element.AmountInArear)); //开票应收款总额
    children.add(_addItem(element.Arrears)); //开票应收款余额
    children.add(_addItem(element.AdvancePaymentAmount)); //预付款总额
    children.add(_addItem(element.AdvancePaymentArrears)); //预付款余额
    children.add(_addItem(element.AdvancePaymentType)); //预付款冲抵类型
    children.add(_addItem(element.AdvancePaymentOffset)); //预付款冲抵
    children.add(_addItem(element.Erate)); //汇率
    children.add(_addItem(element.ColAmount)); //收款金额
    children.add(_addItem(element.Intro)); //备注
    return children;
  }

  ///合计
  static List<Widget> _aCombined(
      BuildContext context, List<PayNoticeDetail> datas) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    children.add(_addItem("")); //合同编号
    children.add(_addItem("")); //合同外部编号
    children.add(_addItem("")); //项目名称
    children.add(_addItem("")); //销售经理
    children.add(_addItem("")); //客服经理
    children.add(_addItem("")); //最后发货时间
    children.add(_addItem("")); //收款超期时间
    children.add(_addItem("")); //提成扣除比列
    children.add(_addItem("")); //收款类型
    children.add(_addItem("")); //收款方式
    double Receivables = 0;
    datas?.forEach((element) {
      var str = element.Receivables;
      Receivables = AcombinedUtils.add(Receivables, str);
    });
    children.add(_addItem(AcombinedUtils.getMoney(Receivables))); //fixme 实付金额
    double AmountInArear = 0;
    datas?.forEach((element) {
      var str = element.AmountInArear;
      AmountInArear = AcombinedUtils.add(AmountInArear, str);
    });
    children
        .add(_addItem(AcombinedUtils.getMoney(AmountInArear))); //fixme 开票应收款总额
    double Arrears = 0;
    datas?.forEach((element) {
      var str = element.Arrears;
      Arrears = AcombinedUtils.add(Arrears, str);
    });
    children.add(_addItem(AcombinedUtils.getMoney(Arrears))); //fixme 开票应收款余额
    double AdvancePaymentAmount = 0;
    datas?.forEach((element) {
      var str = element.AdvancePaymentAmount;
      AdvancePaymentAmount = AcombinedUtils.add(AdvancePaymentAmount, str);
    });
    children.add(
        _addItem(AcombinedUtils.getMoney(AdvancePaymentAmount))); //fixme 预付款总额
    double AdvancePaymentArrears = 0;
    datas?.forEach((element) {
      var str = element.AdvancePaymentArrears;
      AdvancePaymentArrears = AcombinedUtils.add(AdvancePaymentArrears, str);
    });
    children.add(
        _addItem(AcombinedUtils.getMoney(AdvancePaymentArrears))); //fixme 预付款余额
    children.add(_addItem("")); //预付款冲抵类型
    children.add(_addItem("")); //预付款冲抵
    children.add(_addItem("")); //汇率
    double ColAmount = 0;
    datas?.forEach((element) {
      var str = element.ColAmount;
      ColAmount = AcombinedUtils.add(ColAmount, str);
    });
    children.add(_addItem(AcombinedUtils.getMoney(ColAmount))); //fixme 收款金额
    children.add(_addItem("")); //备注
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false, GestureTapCallback? onTap}) {
    Widget child = Center(child: _text(text, isBlue: isBlue));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text?.trim() ?? "",
      style: style,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }
}
